<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="value"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      :disabled="value === ''"
      class="post-btn"
      @click="clickPost"
    >发布</van-button>
  </div>
</template>

<script>
import { postComment } from '@/api/commen.js'
export default {
  data() {
    return {
      value: ''
    }
  },

  props: {
    // 文章id 评论id
    target: {
      type: [Object, Number, String],
      required: true
    },
    // 只需要对评论进行评论才需要
    art_id: {
      type: [Object, Number, String],
      default: null
    }
  },

  created() {

  },

  methods: {
    async clickPost() {
      this.$toast.loading({
        message: '发布中...',
        duration: 0, // 持续展示 toast
        forbidClick: true
      })
      try {
        const res = await postComment({
          target: this.target,
          art_id: this.art_id,
          content: this.value
        })
        console.log(res)
        // 将res.data.new_obj 追加给list
        this.$toast.success('发布成功')
        this.value = ''
        this.$emit('on-post-success', res.data.new_obj)
      } catch (err) {
        this.$toast('评论失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
